// Copyright 2014 Zenasoft.  All rights reserved.
//
// This file is part of Hyperstore.
//
//    Hyperstore is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Hyperstore is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Hyperstore.  If not, see <http://www.gnu.org/licenses/>.

#region Imports

using System;

#endregion

namespace Hyperstore.Modeling
{
    ///-------------------------------------------------------------------------------------------------
    /// <summary>
    ///  A diagnostic message.
    /// </summary>
    ///-------------------------------------------------------------------------------------------------
    [PublicAPI]
    public class DiagnosticMessage
    {
        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Initializes a new instance of the <see cref="DiagnosticMessage"/> class.
        /// </summary>
        /// <param name="messageType">
        ///  Type of the message.
        /// </param>
        /// <param name="message">
        ///  The message.
        /// </param>
        /// <param name="category">
        ///  The category.
        /// </param>
        /// <param name="modelElement">
        ///  (Optional) The model element.
        /// </param>
        /// <param name="ex">
        ///  (Optional) The ex.
        /// </param>
        /// <param name="propertyName">
        ///  (Optional) The name of the property.
        /// </param>
        ///-------------------------------------------------------------------------------------------------
        public DiagnosticMessage(MessageType messageType, string message, string category, IModelElement modelElement = null, Exception ex = null, string propertyName = null)
        {
            DebugContract.RequiresNotEmpty(message);

            MessageType = messageType;
            Message = message;
            Category = category;
            Element = modelElement;
            Exception = ex;
            PropertyName = propertyName;
        }

        internal DiagnosticMessage(MessageType messageType, string message, string category, bool isConstraintMessage, IModelElement modelElement = null, Exception ex = null, string propertyName = null)
            : this(messageType, message, category, modelElement, ex, propertyName)
        {
            DebugContract.RequiresNotEmpty(message);
            IsConstraintMessage = isConstraintMessage;
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Message type.
        /// </summary>
        /// <value>
        ///  The type of the message.
        /// </value>
        ///-------------------------------------------------------------------------------------------------
        public MessageType MessageType { get; private set; }

        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Text of the message.
        /// </summary>
        /// <value>
        ///  The message.
        /// </value>
        ///-------------------------------------------------------------------------------------------------
        public string Message
        {
            get;
            private set;
        }

        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Message category like Rules or Validations.
        /// </summary>
        /// <value>
        ///  The category.
        /// </value>
        ///-------------------------------------------------------------------------------------------------
        public string Category { get; private set; }

        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Gets a value indicating whether the message has been generated by a constraint.
        /// </summary>
        /// <value>
        ///  <c>true</c> if [is constraint message]; otherwise, <c>false</c>.
        /// </value>
        ///-------------------------------------------------------------------------------------------------
        public bool IsConstraintMessage { get; private set; }

        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  The exception is any.
        /// </summary>
        /// <value>
        ///  The exception.
        /// </value>
        ///-------------------------------------------------------------------------------------------------
        public Exception Exception { get; private set; }

        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Gets or sets the element.
        /// </summary>
        /// <value>
        ///  The element.
        /// </value>
        ///-------------------------------------------------------------------------------------------------
        public IModelElement Element { get; private set; }

        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Gets or sets the name of the property.
        /// </summary>
        /// <value>
        ///  The name of the property.
        /// </value>
        ///-------------------------------------------------------------------------------------------------
        public string PropertyName { get; private set; }

        ///-------------------------------------------------------------------------------------------------
        /// <summary>
        ///  Returns a string that represents the current object.
        /// </summary>
        /// <returns>
        ///  A string that represents the current object.
        /// </returns>
        ///-------------------------------------------------------------------------------------------------
        public override string ToString()
        {
            return String.Format("[{0}] - {1} {2}", MessageType, Message,
                Exception != null ? String.Format("{0} : {1}", Exception.GetType().Name, Exception.Message) : String.Empty
                );
        }
    }
}